package main.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import main.constraint.XDF;

public class DialogXDF extends JDialog implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private JTextField name = new JTextField();
	private JTextField context = new JTextField();
	private JTextField lf = new JTextField();
	private JTextField typeLf = new JTextField("(v or n with commas ex.'v,n,v')");	
	private JTextField rg = new JTextField();
	private JComboBox typeRight = new JComboBox();;    
	private JButton btnOK = new JButton("Ok");
    private JButton btnCancelar = new JButton("Cancelar");
    private JPanel botoes = new JPanel();
    private JPanel painel = new JPanel();
    private JPanel grid = new JPanel();

	private XDF xdf = null;
    
    public DialogXDF(){        
        super(Initial3.app,"XML Constraints Validation", false);
    	
    	btnOK.setPreferredSize(new Dimension(100, 25));
        btnOK.addActionListener(this);
        
        btnCancelar.setPreferredSize(new Dimension(100, 25));
        btnCancelar.addActionListener(this);
        
        typeRight.addItem("Value");
        typeRight.addItem("Node");    
        
        typeLf.addFocusListener(new FocusListener(){
            @Override
            public void focusGained(FocusEvent e){
            	if(typeLf.getText().equals("(v or n with commas ex.'v,n,v')"))
            		typeLf.setText("");
            }
            
			@Override
			public void focusLost(FocusEvent e) {
				if(typeLf.getText().equals(""))
					typeLf.setText("(v or n with commas ex.'v,n,v')");			
			}
        });
        
        name.setPreferredSize(new Dimension(200, 25));
        name.setMaximumSize(new Dimension(200, 25));

        grid.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        grid.setLayout(new GridLayout(6, 2, 5, 5));
        grid.add(new JLabel("XFD name:"));
        grid.add(name);            
        grid.add(new JLabel("Context Path:"));
        grid.add(context);                
        grid.add(new JLabel("Left Path:"));
        grid.add(lf);        
        grid.add(new JLabel("Type of equality Left:"));
        grid.add(typeLf);        
        grid.add(new JLabel("Right Path:"));
        grid.add(rg);  
        grid.add(new JLabel("Type of equality Right:"));
        grid.add(typeRight);
        painel.add(grid);        

        botoes.add(btnOK);
        botoes.add(btnCancelar);
        botoes.add(Box.createVerticalStrut(45));
        botoes.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0,Color.LIGHT_GRAY));
        
        this.add(painel, BorderLayout.CENTER);
        this.add(botoes, BorderLayout.SOUTH);
        this.setSize(550, 300);
        this.setLocation((java.awt.Toolkit.getDefaultToolkit()
                                        .getScreenSize().width / 2)
                                        - (this.getWidth() / 2), (java.awt.Toolkit
                                        .getDefaultToolkit().getScreenSize().height / 2)
                                        - (this.getHeight() / 2));
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);    	       
    }
    
    public XDF getXdf() {
		return xdf;
	}

	public void setXdf(XDF xdf) {
		this.xdf = xdf;
	}    
	
	public void clean(){
		name.setText("");
		context.setText("");
		lf.setText("");
		typeLf.setText("(v or n with commas ex.'v,n,v')");
		rg.setText("");
		typeRight.setSelectedItem("Value");
		xdf = null;
	}
    
	@Override
	public void actionPerformed(ActionEvent e) {	
        Object origem = e.getSource();
        
        if(origem == btnOK){		
        	if(!name.getText().equals("") && !context.getText().equals("") && !lf.getText().equals("") && !rg.getText().equals("")
        	   && !typeLf.getText().equals("(v or n with commas ex.'v,n,v')"))
        	{
	        	xdf = new XDF();
				xdf.setNameConstraint(name.getText());
				xdf.setContextFsa(context.getText());
				xdf.setSourceFsa(lf.getText());
				xdf.setEquality(typeLf.getText(),"l");
				xdf.setTargetFsa(rg.getText());
				
				if(typeRight.getSelectedIndex() == 0)			
					xdf.setEquality("v","r");
				else
					xdf.setEquality("n","r");
								
				Initial3.ke.addKeys(xdf);						
				Initial3.t2.setText(Initial3.ke.toString());
				Initial3.kTree.setEnabled(true);    
				
				this.dispose();
        	} else
                JOptionPane.showMessageDialog(this,"Fill in all fields.", "Attention", JOptionPane.ERROR_MESSAGE);                                                                        
        }        
        
        if (origem == btnCancelar)
            this.dispose();		
	}    
}
